<?php
namespace App\Http\Controllers\Backstage;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Validator\MoveRuleVali;
use App\Http\Controllers\Validator\RoomInfoVail;
use App\Http\Controllers\Validator\RoomWordbookVail;
use App\Model\Account\Type;
use App\Model\Backstage\Customers;
use App\Model\Backstage\MoveRule;
use App\Model\Backstage\MoveRuleStatus;
use App\Model\Backstage\RoomInfo;
use App\Model\Backstage\RoomWordbook;
use App\Model\Backstage\RoomWordbookInfo;
use App\Model\Backstage\User;
use App\Tool\Error;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Validator;
use Illuminate\Support\Facades\Auth;
use App\Model\Account\SystemType;

/**
 * Class MoveRuleController
 * @package App\Http\Controllers\Backstage
 * 公司房客自动转移规则
 * date:2018-4-29
 */
class MoveRuleController extends Controller
{

    /**
     * @return \Illuminate\Http\JsonResponse
     * 添加页面所需动态数据
     */
    public function dataList(){
        $data = [
            'move_type' => MoveRule::$moveTypeArr,
            'date_type' => MoveRule::$dateTypeArr,

        ];
        //房
        $data['r_transaction'] =  SystemType::storageRedis(92);//交易
        $data['r_status'] =  SystemType::storageRedis(96);//状态
        $data['r_use'] =  SystemType::storageRedis(1);//用途
        $data['r_delegation'] =  SystemType::storageRedis(111);//委托方式
        //客
        $data['c_transaction'] =  SystemType::storageRedis(450);//交易
        $data['c_status'] = SystemType::storageRedis(454);//客源状态
        $data['c_use'] = SystemType::storageRedis(1);//用途
        $data['signing'] = SystemType::storageRedis(515);//签约方式
        return $this->success('ok', $data);
    }

    /**
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 规则数据列表
     */
    public function lists(MoveRule $moveRule){
        $list = $moveRule->lists();
        if(!$list){
            return $this->error(Error::NULL_DATA);
        }
        foreach($list as $k => $v){
            $v->a_date_type_name = MoveRule::$dateTypeArr[$v->a_date_type]; //日期类型
            $v->a_move_type_name = MoveRule::$moveTypeArr[$v->a_move_type]; //转移类型
            $v->a_trade_type_name = SystemType::getTypeDetail($v->a_trade_type); //交易类型
            $v->a_m_status_name   = SystemType::getTypeDetail($v->a_m_status); //房源状态
            $v->a_use_name        = SystemType::getTypeDetail($v->a_use_type); //用途
            $entrust_way_id = explode(',', $v->a_entrust_way);
            $entrust_way_arr = [];
            foreach($entrust_way_id as $value){
                $temp_arr['id'] = $value;
                $temp_arr['name'] = SystemType::getTypeDetail($value);
                array_push($entrust_way_arr, $temp_arr);
            }
            $v->a_entrust_way     = $entrust_way_arr;
            $v->a_status_name     = $v->a_status == 1 ? '启用' : '禁用';
        }
        return $this->success('ok', $list);

    }

    /**
     * @param Request $request
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 添加规则
     */
    public function addRule(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::validate($params)){
            return $this->error(Error::PARAM_MISSING);
        }
        $params['a_entrust_way'] = implode(',', $params['a_entrust_way']);
        $status = $moveRule->addRule($params);
        if ($status === -1) {
            return $this->error(Error::RULE_ALREADY);
        }
        if($status){
            return $this->success('ok', '添加成功！');
        }
        return $this->error(Error::ADD_ERROR);

    }
    /**
     * @param Request $request
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 编辑规则
     */
    public function editRule(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::validate($params) || !$request->input('a_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $params['a_entrust_way'] = implode(',', $params['a_entrust_way']);
        $status = $moveRule->updateRule($params);
        if ($status === -1) {
            return $this->error(Error::RULE_ALREADY);
        }
        if($status){
            return $this->success('ok', '修改成功！');
        }
        return $this->error(Error::EDIT_ERROR);
    }

    /**
     * @param Request $request
     * @param MoveRule $moveRule
     * @return \Illuminate\Http\JsonResponse
     * 删除规则
     */
    public function delRule(Request $request, MoveRule $moveRule){
        if(!$request->input('a_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        if($moveRule->delRule($request->input('a_id'))){
            return $this->success('ok', '删除成功！');
        }
        return $this->error(Error::DEL_ERROR);
    }

    //手动立即执行所有开启的规则
    public function ExecutionRule(MoveRule $moveRule, RoomInfo $roomInfo, Customers $customers){
        $info = $moveRule->ExecutionRule();
        if(!$info){
            return $this->error(Error::OPERATION_ERROR);
        }
        $res = [];
       foreach($info as $key=>$value){
           if($value['a_move_type'] == 0 || $value['a_move_type'] == 2){  //私盘转公盘  取消优质房
             $res[] = $roomInfo->updateRuleDisk($value);

            }elseif($value['a_move_type'] == 1){  //私客转公客
               $res[] = $customers->updateRuleCustomers($value);
           }

       }
//       if(!in_array('0',$res)){
           return $this->success('ok', '操作成功！');
//       }
//        return $this->error(Error::OPERATION_ERROR);
    }

    /**
     *用于手动即时更新规则调用
     */
    public function immediateSave(Request $request, RoomInfo $roomInfo, Customers $customers){
        if(!$request->input('a_id')){
            return $this->error(Error::PARAM_MISSING);
        }
        $MoveRule = new MoveRule();
        $info = $MoveRule->getInfo($request->input('a_id'));
        if ($info->a_status == 0) { //禁用的规则不能执行
            return $this->error(Error::RULE_DISABLE);
        }
        $info = self::object_to_array($info);
        if($info['a_move_type'] == 0 || $info['a_move_type'] == 2){  //私盘转公盘  取消优质房
            $roomInfo->updateRuleDisk($info);
        }elseif($info['a_move_type'] == 1){  //私客转公客
            $customers->updateRuleCustomers($info);
        }
        return $this->success('ok', '操作成功！');
    }
    /**
     * 对象 转 数组
     *
     * @param object $obj 对象
     * @return array
     */
    function object_to_array($obj) {
        $obj = (array)$obj;
        foreach ($obj as $k => $v) {
            if (gettype($v) == 'resource') {
                return;
            }
            if (gettype($v) == 'object' || gettype($v) == 'array') {
                $obj[$k] = (array)object_to_array($v);
            }
        }
        return $obj;
    }

    /**
     * @param MoveRuleStatus $moveRuleStatus
     * @param MoveRule $moveRule
     * @param RoomInfo $roomInfo
     * @param Customers $customers
     * @return \Illuminate\Http\JsonResponse
     * 此接口需要员工登陆完成后调用此方法
     * 执行公司自定义房客更新规则
     */
    public function updateRule(MoveRuleStatus $moveRuleStatus, MoveRule $moveRule, RoomInfo $roomInfo, Customers $customers)
    {
        $res = $moveRuleStatus->getStatus($moveRule, $roomInfo, $customers);
        if($res){
            return $this->success('ok');
        }
        return $this->error(Error::EDIT_ERROR);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 转出类目列表数据
     */
    public function moveData(){

        $area_list = DB::table('area')->where(['parentId' => $this->city_code])->get();
        $data = [
            'movelistarr' => MoveRule::$moveListArr,
            'province_code'     => $this->province_code,//省code
            'city_code'         => $this->city_code,//市code
            'city_name'         => $this->city,//市名字
             'area_list'         => $area_list,
        ];
        $data['r_transaction'] =  SystemType::storageRedis(92);//交易
        $data['r_status'] =  SystemType::storageRedis(96);//状态
        $data['c_transaction'] =  SystemType::storageRedis(450);//交易
        $data['c_status'] = SystemType::storageRedis(454);//客源状态

        return $this->success('ok', $data);
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * 员工转房客
     */
    public function moves(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::moveValidate($params)){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $moveRule->moves($params);
        if($res){
            return $this->success('ok', '操作成功');
        }
        return $this->error(Error::OPERATION_ERROR);
    }

    /**
     * @return \Illuminate\Http\JsonResponse
     * 转换页面需要下拉数据
     */
    public function turnData(){
        $area_list = DB::table('area')->where(['parentId' => $this->city_code])->get();
        $data = [
            'turntype' => MoveRule::$turnArr,
            'datetype' => MoveRule::$dateTypeArr,
            'province_code'     => $this->province_code,//省code
            'city_code'         => $this->city_code,//市code
            'city_name'         => $this->city,//市名字
            'area_list'         => $area_list,
        ];
        $data['r_transaction'] =  SystemType::storageRedis(92);//交易
        $data['r_status'] =  SystemType::storageRedis(96);//状态
        $data['c_transaction'] =  SystemType::storageRedis(450);//交易
        $data['c_status'] = SystemType::storageRedis(454);//客源状态

        return $this->success('ok', $data);
    }

    /**
     * @param Request $request
     * 员工公私盘 公私客相互转换
     */
    public function turns(Request $request, MoveRule $moveRule){
        $params = $request->except('token');
        if(!MoveRuleVali::turnValidate($params)){
            return $this->error(Error::PARAM_MISSING);
        }
        $res = $moveRule->turns($params);
        if($res === -1){
            return $this->error(Error::OPERATION_ERROR);
        }

        return $this->success('ok', '操作成功！');
    }


}